package demo3;

class Money /*implements Cloneable*/{
    public double money = 20.2;

    /*@Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }*/
}

class Person implements Cloneable{
    int age;
    public Money m;//组合
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }

    public Person(int age) {
        this.age = age;
        this.m = new Money();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
        /*Person tmp = (Person) super.clone();
        tmp.m = (Money) this.m.clone();
        return tmp;*/
    }
}

public class Test02 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person(10);
        Person person2 = (Person) person1.clone();
        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person1.age);
        System.out.println(person2.age);
        person1.age = 20;
        System.out.println(person1.age);
        System.out.println(person2.age);

        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        System.out.println("=============");
        person1.m.money = 25.25;
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
    }

}
